package cn.diancan.service.impl;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import cn.diancan.dao.MyBaseDao;
import cn.diancan.exception.OutMsgException;
import cn.diancan.model.MealLocation;
import cn.diancan.model.PurchaseSaleOrder;
import cn.diancan.model.types.CommonType;
import cn.diancan.model.types.MealLocationStatusType;
import cn.diancan.service.MealLocationService;
import cn.diancan.service.PurchaseSaleOrderService;
import cn.diancan.vo.MealLocationVo;
import cn.diancan.vo.other.Pager;
import cn.diancan.vo.other.ResultVoUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Component;


@Component("mealLocationService")
public class MealLocationServiceImpl implements MealLocationService {

    @Resource(name = "MyBaseDao")
    private MyBaseDao<MealLocation> mealLocationDao;

    @Override
    public boolean saveMealLocation(MealLocation mealLocation)   throws OutMsgException, Exception {
		if (null == mealLocation) {
			throw new OutMsgException("参数错误");
		}
		mealLocation.init();
		mealLocation.setLocationStatus(MealLocationStatusType.FREE.getType());
		mealLocation.setIsEnable(CommonType.Enable.YES);
		mealLocation.setIndexNo(10);

		mealLocationDao.save(mealLocation);
		return true;
    }

    @Override
    public boolean deleteMealLocationById(Long mealLocationId)    throws OutMsgException, Exception {
		if (null == mealLocationId || mealLocationId <= 0L) {
		    throw new OutMsgException("参数错误");
		}
		MealLocation mealLocation = findbyMealLocationId(mealLocationId);
		if (null == mealLocation) {
		    throw new OutMsgException("参数错误");
		}

		mealLocation.init();
		mealLocationDao.update(mealLocation);
		return true;
    }

    @Override
    public boolean updateMealLocation(MealLocation mealLocation)    throws OutMsgException, Exception {
		if (null == mealLocation) {
		    throw new OutMsgException("参数错误");
		}
		MealLocation mealLocation1 = mealLocationDao.get(MealLocation.class, mealLocation.getId());

		BeanUtils.copyProperties(mealLocation1,mealLocation);

		mealLocationDao.update(mealLocation1);
		return true;
    }

    @Override
    public MealLocation findbyMealLocationId(Long mealLocationId)    throws OutMsgException, Exception {
		if (null == mealLocationId || mealLocationId <= 0L) {
		    throw new OutMsgException("参数错误");
		}
		return mealLocationDao.get("FROM MealLocation WHERE id=? AND isDelete=?",	new Object[] { mealLocationId, CommonType.Delete.NO });
    }


	@Resource(name = "MyBaseDao")
    private MyBaseDao<PurchaseSaleOrder> purchaseSaleOrderDao;

	@Override
	public MealLocationVo findVobyMealLocationId(Long mealLocationId) throws OutMsgException, Exception {
		MealLocation mealLocation = findbyMealLocationId(mealLocationId);
		MealLocationVo vo = new MealLocationVo();
		BeanUtils.copyProperties(vo,mealLocation);

		if(null != mealLocation && null!= mealLocation.getPurchaseSaleOrderId()){
			PurchaseSaleOrder purchaseSaleOrder = purchaseSaleOrderDao.get(PurchaseSaleOrder.class, mealLocation.getPurchaseSaleOrderId());
			if(null!=purchaseSaleOrder){
				BeanUtils.copyProperties(vo,purchaseSaleOrder);
			}
		}
		return vo;
	}

	@Override
    public Pager findMealLocationPager(Pager pager)    throws OutMsgException, Exception {
		Integer pageNo = pager.getPageNumber();
		Integer row = pager.getPageSize();
		List<Object> param = new ArrayList<Object>();
		param.add(CommonType.Delete.NO);
		String hql = "FROM MealLocation WHERE isDelete=? ";
		List<MealLocation> mealLocations = mealLocationDao.find(hql, param,	pageNo, row);
		pager.setList(mealLocations);

		Long count = mealLocationDao.count("SELECT COUNT(id) " + hql, param);
		pager.setTotalCount(count.intValue());
		return pager;
    }

    @Override
    public List<MealLocation> findMealLocationList(Integer pageNo, Integer row)	    throws OutMsgException, Exception {
		return mealLocationDao.find("FROM MealLocation WHERE isDelete=? ",new Object[] { CommonType.Delete.NO }, pageNo, row);
    }

    /*
        private Long mealLocationId;
    private String locationName;//1号桌  临时1号  外送
    private Integer locationType; //1.固定位置  2.临时添加位置  3.外送
    private Integer locationStatus; //使用状态  1.无人  2.制作中 3.用餐中
    private Long purchaseSaleOrderId; //当前桌的消费情况
    private Integer isEnable;
    private String outOrderNumber;
     */
	@Override
	public List<MealLocationVo> findMealLocationVoList(Integer pageNo, Integer row) throws OutMsgException, Exception {
		String hql = "Select m1.id AS mealLocationId,m1.locationName,m1.locationType,m1.locationStatus,m1.purchaseSaleOrderId,p2.outOrderNumber "+
 				" FROM MealLocation AS m1 LEFT JOIN PurchaseSaleOrder AS p2 ON m1.purchaseSaleOrderId=p2.id  " ;
				/*"WHERE m1.purchaseSaleOrderId is NOT NULL  AND m1.purchaseSaleOrderId!='0' AND m1.locationStatus!=? ";*/

		List<Object> param = new ArrayList<Object>();
		/*param.add(MealLocationStatusType.FREE.getType());*/

		hql+=" Order by m1.indexNo ASC ";
		List<Object[]> lsObj = mealLocationDao.findSql(hql, param, pageNo, row);
		if(null ==lsObj || lsObj.size()<=0){
			return null;
		}
		return ResultVoUtil.object2Model(MealLocationVo.class,lsObj,hql);
	}




}
